from typing import *


class Solution:
    def countKConstraintSubstrings(
        self, s: str, k: int, queries: List[List[int]]
    ) -> List[int]:
        l = a = b = 0
        ans = 0
        for i, v in enumerate(s):
            if v == "0":
                a += 1
            else:
                b += 1
            while a > k and b > k:
                lv = s[l]
                if lv == "0":
                    a -= 1
                else:
                    b -= 1
                l += 1
            ans += i - l + 1
        return ans
